<?php

class DepartmentController extends AController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='/layouts/column6';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
			//'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('allow',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model = new UserDepartment;
		$statusOption = $model->statusOption();
		$ynOption = $model->ynOption();
		$attrOption = $model->attrOption();
		if(isset($_POST['action']) && $_POST['action']=='create')
		{
			if(isset($_POST['parent_id_1'])&&($_POST['parent_id_1']!=0))
				$model->parent_id = (int)$_POST['parent_id_1'];
			if(isset($_POST['parent_id_2'])&&($_POST['parent_id_2']!=0))
				$model->parent_id = (int)$_POST['parent_id_2'];
			if(isset($_POST['parent_id_3'])&&($_POST['parent_id_3']!=0))
				$model->parent_id = (int)$_POST['parent_id_3'];

			$model->level = $_POST['level'];
			//$model->project_group = $_POST['project_group'];
			$model->name = $_POST['name'];
			$model->manager = $_POST['manager'];
			$model->manager_unique_id = $_POST['manager_unique_id'];
			$model->status = $_POST['status'];
			$model->attribute = $_POST['attribute'];
			$model->display = $_POST['display'];
			$model->dateline = $_SERVER['REQUEST_TIME'];
			$model->admin_assistant = $_POST['admin_assistant'];
			$model->hr_assistant = $_POST['hr_assistant'];
			if($model->save())
				$this->showAlert(array('type'=>'1','content'=>'添加成功！','url'=>'admincp/department/index'));
		}

		//获取第一级别的部门
		$result = $model->findAllByAttributes(array('level'=>1,'status'=>'1'));
		$d1[0] = '1级';
		foreach ($result as $value)
		{
			$d1[$value['id']] = $value['name'];
		}

		$this->render('create',array(
			'model'=>$model, 'statusOption'=>$statusOption,'ynOption'=>$ynOption,'attrOption'=>$attrOption,'d1'=>$d1
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$id = $id?$id:$_POST['id'];
		$model = UserDepartment::model()->findByPk($id);
		$statusOption = $model->statusOption();
		$ynOption = $model->ynOption();
		$attrOption = $model->attrOption();
		$model->getPath($model->parent_id);
		$path =$model->departmentPath;

		//print_r($path);

		if(empty($path))
		{
			$departmentOptionsVal[1] = 0;
			$departmentOptions[1] = $model->getOptionsByPid(0);
		}
		else
		{
			$j = 1;
			for($i=count($path)-1; $i >= 0; $i--) 
			{
				$departmentOptionsVal[$j] = $path[$i]['id'];
				$departmentOptions[$j] = $model->getOptionsByPid($path[$i]['parent_id']);
				$j++;
			}
		}

		if(isset($_POST['action']) && ($_POST['action'] == 'update'))
		{
			if(isset($_POST['parent_id_1']))
				$model->parent_id = (int)$_POST['parent_id_1'];
			if(isset($_POST['parent_id_2'])&&($_POST['parent_id_2']!=0))
				$model->parent_id = (int)$_POST['parent_id_2'];
			if(isset($_POST['parent_id_3'])&&($_POST['parent_id_3']!=0))
				$model->parent_id = (int)$_POST['parent_id_3'];
			$model->level = (int)$_POST['level'];
			//$model->project_group = $_POST['project_group'];
			$model->name = $_POST['name'];
			$model->manager = $_POST['manager'];
			$model->manager_unique_id = $_POST['manager_unique_id'];
			$model->status = $_POST['status'];
			$model->attribute = $_POST['attribute'];
			$model->display = $_POST['display'];
			$model->dateline = $_SERVER['REQUEST_TIME'];
			$model->admin_assistant = $_POST['admin_assistant'];
			$model->hr_assistant = $_POST['hr_assistant'];
			if($model->save())
				$this->showAlert(array('type'=>'1','content'=>'更新成功！','url'=>'admincp/department/update/id/'.$id));
			else
				var_dump($model->getErrors());
		}
		
		
		$this->render('update',array(
			'model'=>$model, 'statusOption'=>$statusOption,'ynOption'=>$ynOption,'attrOption'=>$attrOption,'departmentOptionsVal'=>$departmentOptionsVal,'departmentOptions'=>$departmentOptions
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}


	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model = new UserDepartment;
		$model->getTree(0 , 1 , 0);
		//print_r($model->departmentTree);
		$this->render('index',array(
			'tree'=>$model->departmentTree,
		));
		
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=UserDepartment::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}



	/**
	 * AJAX 获取部门树形结构
	 * 
	 */
	public function actionGetDepartmentTree()
	{


	}


	/**
	 * CODE列表页
	 */
	public function actionCode()
	{
		$dataProvider=new CActiveDataProvider('UserCode',array(
			'criteria'=>array(
				'condition'=>"status=1  ORDER BY code ASC"
				),
			'pagination'=>array(
		        'pageSize'=>80,
		    ),
		));
		$this->render('code_index',array(
			'dataProvider'=>$dataProvider,
		));
	}


	public function actionCodeCreate()
	{
		$userDepartmentModel = new UserDepartment;
		$model = new UserCode;
		$statusOption = $userDepartmentModel->statusOption();

		if(isset($_POST['action']) && $_POST['action']=='create')
		{
			$model->code = $_POST['code'];
			$model->name = $_POST['name'];
			$model->manager_unique_id = $_POST['manager_unique_id'];
			$model->manager = $_POST['manager'];
			$model->c_name = $_POST['c_name'];
			$model->status = $_POST['status'];
			if($model->save())
				$this->showAlert(array('type'=>'1','content'=>'添加成功！','url'=>'admincp/department/code'));
		}

		$this->render('code_create',array(
			'model'=>$model,'statusOption'=>$statusOption
		));


	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionCodeUpdate($id)
	{
		$userDepartmentModel = new UserDepartment;
		$statusOption = $userDepartmentModel->statusOption();
		if(isset($_POST['action']) && $_POST['action'] == 'update')
		{
			$model = UserCode::model()->findByPk($_POST['id']);
			$model->code = $_POST['code'];
			$model->name = $_POST['name'];
			$model->manager_unique_id = $_POST['manager_unique_id'];
			$model->manager = $_POST['manager'];
			$model->c_name = '2';
			$model->status = $_POST['status'];
			$model->save();
				$this->showAlert(array('type'=>'1','content'=>'更新成功！','url'=>'admincp/department/codeUpdate/id/'.$id));
		}
		else
		{
			$model = UserCode::model()->findByPk($id);
		}

		$this->render('code_update',array(
			'model'=>$model,'statusOption'=>$statusOption
		));		
	}



}
